package com.mhc.suzuki.controller;

import com.mhc.haval.security.model.User;
import com.mhc.suzuki.bo.MessageBO;
import com.mhc.suzuki.bo.MessageQueryBO;
import com.mhc.suzuki.constant.ErrorCodeEnum;
import com.mhc.suzuki.constant.MessageStatusEnum;
import com.mhc.suzuki.form.MessageQueryForm;
import com.mhc.suzuki.service.MessageService;
import com.mhc.suzuki.util.DateUtil;
import com.mhc.suzuki.vo.MessageVO;
import com.subaru.common.entity.BizResult;
import com.subaru.common.util.BeanCopierUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Created by HW
 * on 2017/9/8 19:08.
 */

@Slf4j
@RestController
@RequestMapping(value = "/messages")
public class MessagesController {

    @Autowired
    private MessageService messageService;

    /**
     * 条件查询消息列表
     *
     * @param messageQueryForm
     * @return
     */
    @ApiOperation(value = "我的消息标记列表", notes = "条件获取我的消息标")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public BizResult<List<MessageVO>> changeStatus(MessageQueryForm messageQueryForm, @AuthenticationPrincipal User user) {
        Optional<User> userOptional = Optional.ofNullable(user);
        if (!userOptional.isPresent()) {
            return BizResult.create(ErrorCodeEnum.NOT_LOGIN.getErrMsg(), ErrorCodeEnum.NOT_LOGIN.getErrCode());
        } else {
            if (messageQueryForm.getPageNo() == null || messageQueryForm.getPageNo() == 0) {
                messageQueryForm.setPageNo(1);
            }
            if (messageQueryForm.getPageSize() == null || messageQueryForm.getPageSize() == 0) {
                messageQueryForm.setPageSize(10);
            }
            BizResult<List<MessageVO>> bizResult;
            log.info(messageQueryForm.toString());
            List<MessageBO> messagesBOS;

            log.info("开始查询用户{}的消息列表", user.getUsername());
            MessageQueryBO messageQueryBO = BeanCopierUtil.convert(messageQueryForm, MessageQueryBO.class);

        /*"2017-09-13"字符串转换为localdatetime*/
            if (StringUtils.isNotBlank(messageQueryForm.getGmtEnd()) && StringUtils.isNotBlank(messageQueryForm.getGmtStart())) {
                String end = messageQueryForm.getGmtEnd();
                end += " 23:59:59";
                String start = messageQueryForm.getGmtStart();
                start += " 00:00:00";
                LocalDateTime s = DateUtil.defaultParseStringToDateTime(start);
                LocalDateTime e = DateUtil.defaultParseStringToDateTime(end);
                messageQueryBO.setGmtStart(s);
                messageQueryBO.setGmtEnd(e);
            }
            messageQueryBO.setStaffId(user.getStaffId());
            //获取未删除的消息列表
            messageQueryBO.setStatus(MessageStatusEnum.DELETE.getCode());
            messagesBOS = messageService.getMessagesNotDeletedByQueryBO(messageQueryBO);
            int count = messageService.getMessagesCountNotDeletedByQueryBO(messageQueryBO);

        /*BO->VO*/
            List<MessageVO> messageVOS = new ArrayList<>();
            for (MessageBO messageBO : messagesBOS) {
                MessageVO messageVO = BeanCopierUtil.convert(messageBO, MessageVO.class);
                messageVOS.add(messageVO);
            }

            bizResult = BizResult.create(messageVOS, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
            bizResult.setPageNo(messageQueryForm.getPageNo());
            bizResult.setPageSize(messageQueryForm.getPageSize());
            bizResult.setTotalCount(count);
            return bizResult;
        }
    }


    /**
     * 获取用户未读消息总数
     *
     * @return
     */
    @ApiOperation(value = "未读消息总数", notes = "条件获取我的消息总数")
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    public BizResult messagesCount(@AuthenticationPrincipal User user) {
        MessageQueryBO messageBO = new MessageQueryBO();
        messageBO.setStaffId(user.getStaffId());
        messageBO.setStatus(MessageStatusEnum.UNREAD.getCode());
        int result = messageService.getMessagesCountByQueryBO(messageBO);
        return BizResult.create(result, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
    }


    /**
     * 消息标为已读
     *
     * @param messageId 消息id
     * @return
     */
    @ApiOperation(value = "我的消息标记已读", notes = "点击已读，将我的消息标记为已读")
    @RequestMapping(value = "/status/{messageId}", method = RequestMethod.POST)
    public BizResult changeStatus(@PathVariable Long messageId) {
        if (messageId != null && messageService.changeStatus(messageId, MessageStatusEnum.READ.getCode())) {
            return BizResult.create(null, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        }
        return BizResult.create(ErrorCodeEnum.PARAM_ERROR.getErrCode(), ErrorCodeEnum.PARAM_ERROR.getErrMsg());
    }

    /**
     * 删除消息
     *
     * @param messageId 消息id
     * @return
     */
    @ApiOperation(value = "删除消息", notes = "删除消息，将我的消息标记为已删除")
    @RequestMapping(value = "/delete/{messageId}", method = RequestMethod.POST)
    public BizResult deleteMessage(@PathVariable Long messageId) {
        if (messageId != null && messageService.changeStatus(messageId, MessageStatusEnum.DELETE.getCode())) {
            return BizResult.create(null, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        }
        return BizResult.create(ErrorCodeEnum.PARAM_ERROR.getErrCode(), ErrorCodeEnum.PARAM_ERROR.getErrMsg());
    }


    /**
     * 消息标为已读
     *
     * @param messageIdS 消息id列表
     * @return
     */
    @ApiOperation(value = "我的消息批量标记已读", notes = "点击已读，将我的消息标记为已读")
    @RequestMapping(value = "/status", method = RequestMethod.POST)
    public BizResult changeStatus(@RequestBody Long[] messageIdS) {

        if (messageService.changeStatusByList(messageIdS)) {
            return BizResult.create(null, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        }
        return BizResult.create(ErrorCodeEnum.PARAM_ERROR.getErrCode(), ErrorCodeEnum.PARAM_ERROR.getErrMsg());

    }


}